package com.wonderful.eyepetizer.ui.pages.discover

import androidx.compose.foundation.Image
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.Divider
import androidx.compose.material.ExperimentalMaterialApi
import androidx.compose.material.pullrefresh.PullRefreshIndicator
import androidx.compose.material.pullrefresh.pullRefresh
import androidx.compose.material.pullrefresh.rememberPullRefreshState
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.paging.compose.collectAsLazyPagingItems
import coil.compose.rememberAsyncImagePainter
import com.wonderful.eyepetizer.app.ktx.loadMoreView
import com.wonderful.eyepetizer.data.entity.bean.Topic
import com.wonderful.eyepetizer.ui.pages.detail.TopicDetailActivity

/**
 * Desc:
 * @Author: wonderful
 * @Time: 2024/2/22 16:26
 */
@OptIn(ExperimentalMaterialApi::class)
@Composable
fun TopicPage(viewModel: DiscoverViewModel = viewModel()) {
    val lazyPagingItems = viewModel.topicPageFlow.collectAsLazyPagingItems()
    val pullRefreshState = rememberPullRefreshState(
        refreshing = viewModel.topicRefreshing.value,
        onRefresh = { lazyPagingItems.refresh() }
    )
    Box(modifier = Modifier.pullRefresh(pullRefreshState)) {
        LazyColumn(modifier = Modifier.fillMaxSize(), content = {
            items(lazyPagingItems.itemCount) {
                val topic = lazyPagingItems[it]
                TopicItemWidget(topic = topic!!)
            }
            loadMoreView(pagingItems = lazyPagingItems)
        })
        PullRefreshIndicator(
            refreshing = viewModel.topicRefreshing.value,
            state = pullRefreshState,
            modifier = Modifier.align(Alignment.TopCenter)
        )
    }
}

@Composable
fun TopicItemWidget(topic: Topic) {
    val context = LocalContext.current
    Column(
        modifier = Modifier
            .fillMaxWidth()
            .clickable {
                TopicDetailActivity.actionStart(context, topic.data.id)
            }
    ) {
        Image(
            painter = rememberAsyncImagePainter(model = topic.data.image),
            contentDescription = null,
            modifier = Modifier
                .padding(10.dp)
                .fillMaxWidth()
                .height(200.dp)
                .clip(RoundedCornerShape(4.dp)),
            contentScale = ContentScale.Crop
        )
        Divider(thickness = 0.5.dp, modifier = Modifier.padding(horizontal = 10.dp))
    }
}